<template>
	<view class="wrap">
		<u-cell-group>
			<u-cell-item icon="bag" title="订单编号" :arrow="false" :value="orderSn" style="flex-direction: row;"></u-cell-item>
			<u-cell-item icon="rmb" title="应付金额" :arrow="false" :value="totalPriceFmt" style="flex-direction: row;"></u-cell-item>
		</u-cell-group>
		<u-radio-group v-model="payType" style="width:100%; display: inline-flex; flex-wrap: wrap;">
			<view class="item" v-for="(res, index) in payWayList" :key="res.name">
				<u-row>
					<u-col :span="11">
						<view class="top" style="height: 50px;">
							<view class="name" style="padding: 10px 0 0 10px;">
								<u-image mode="widthFix" width="120px" :src="res.img"></u-image>
							</view>
						</view>
					</u-col>
					<u-col :span="1">
						<u-radio :name="res.name"></u-radio>
					</u-col>
				</u-row>
			</view>
		</u-radio-group>

		<u-button type="primary" @click="submit">立即支付</u-button>

	</view>
</template>

<script>
	const db = uniCloud.database()
	const dbCmd = db.command
	const dbOrder = db.collection("t_shop_order")
	
	export default {
		data() {
			return {
				orderSn: '',
				totalPrice: '',
				payType: '',
				payWayList: [{
						name: 'alipay',
						text: '支付宝支付',
						img: '/static/pay/alipay.png'
					},
					{
						name: 'wxpay',
						text: '微信支付',
						img: '/static/pay/wxpay.png'
					},

				]
			}
		},
		computed: {
			totalPriceFmt() {
				console.log(this.totalPrice)
				return '¥' + (this.totalPrice / 100).toFixed(2)
			}
		},
		onLoad(option) {
			this.orderSn = option.orderSn
			this.totalPrice = option.totalPrice
			this.init()
		},
		methods: {
			init() {
				dbOrder.where({order_sn: dbCmd.eq(this.orderSn)}).get().then(res => {
					let order = res.result.data[0]
					if (order.pay_status == "1" && order.status == "1") {
						this.$u.route({
							url: '/pages/order/detail',
							params: {
								orderSn: orderSn
							}
						})
					}
				})
			},
			submit() {
				if (this.payType == 'alipay') {
					dbOrder.where({order_sn: dbCmd.eq(this.orderSn)})
					.update({
						pay_status: "2",
						pay_type: 'alipay',
						status: 2,
						pay_time: Date.now()
					}).then(res => {
						console.log(res);
						this.$u.route({
							url: '/pages/order/detail',
							params: {
								orderSn: this.orderSn
							}
						})
					})
				}
			},
		}
	}
</script>


<style lang="scss" scoped>
	.wrap {
		padding: 0rpx 30rpx;

		.item {
			width: 100%;
			padding: 20rpx 10rpx;
		}
	}
</style>
